// 获取编译启动的默认语言
import Locale from '~/locales'
import Cookies from 'js-cookie'

// 获取编译启动的默认语言
const ENV_LANGUAGE = process.env.LANGUAGE ? process.env.LANGUAGE : 'zh';

export const state = () => ({
  locales: Locale(),
  locale: ENV_LANGUAGE
});

export const mutations = {
  /** 保存全局默认语言 **/
  SET_LANGUAGE(state, locale) {
    if (state.locales.indexOf(locale) !== -1) {
      Cookies.set('language', locale);
      state.locale = locale
    }
  },
};

export const actions = {
    // 系统初始化时(context上下文对象在第二个参数中)
    async nuxtServerInit({ commit, state }, { app, req }) {
      // 在系统初始化时，可以在这里请求某些接口，写入vuex，然后可以在客户端获取vuex数据
      // await fetchMemberInfo().then((res) => {
      //   console.log(res)
      // })
      // await getLanguage().then(res => {
      //   console.log('res :>> ', res);
      // })
      // let b = await app.$axios.get('/api/public/get-language');
      // console.log('state :>> ', state);
      // console.log('language', language)
      /** 从cookie中提取token到vuex **/
      // this.$cookies.get('token') ? commit('SET_TOKEN', this.$cookies.get('token')) : ''
      /** 从cookie中提取language到vuex **/
      this.$cookies.get('language') ? commit('SET_LANGUAGE', this.$cookies.get('language')) : 'zh'
    }
}


